#include "utils.h"
void split(string in,vector<string>& out, char separator)
{
	string::iterator it;
	int offset = 0;
	string temp;
	for ( it=in.begin() ; it < in.end(); it++ )
	{
		if( *it==separator)
		{
			out.push_back(temp);
			temp.clear();
		}
		else
			temp.push_back(*it);		
	}
	out.push_back(temp);
}
float stof(string in)
{
	return atof(in.c_str());
}
int stoi(string in)
{
	return atoi(in.c_str());
}
bool isEqual(float n1, float n2,float epsilon) 
{
  return fabs(n1 - n2) < epsilon;
}
uint floatToFixed(float f,FIXED_POINT_FORMAT type)
{
	int sign_bits = type>>16;
	int integer_bits= type>>8&0xff;
	int fractional_bits = type&0xff;
	int number_bits = integer_bits+fractional_bits;
	float scaleFactor = float(1<<fractional_bits);
	float scale = fabs(f)*scaleFactor;
	unsigned max_number = 1<<number_bits;
	if( scale> max_number)
		cout<<"\nfloatToFixed:Error when convert :"<<f<<" to fixed format: FP_"<<sign_bits<<"_"<<integer_bits<<"_"<<fractional_bits<<" (To big)";
	int flr = scale;
	int sign=0;
	if( f<0.)
		if( sign_bits)
		{
			sign= 1<<number_bits;
		}
		else
		{
			cout<<"\nfloatToFixed:Error when convert :"<<f<<" to fixed format: FP_"<<sign_bits<<"_"<<integer_bits<<"_"<<fractional_bits<<" (f<0. and not have sign bit)";
		}
	long long re  = sign+flr;
	uint r = re;
	return r;
}
FIXED_POINT_FORMAT findFixedPointFormat(arrayVec3& a)
{
	int sign_bit = 0;
	int max_interger = 0;
	int frag_bit;
	for(int i=0;i< a.size();i++)
	{
		float x1 = a[i].x;
		float x2 = a[i].y;
		float x3 = a[i].z;
		if(x1<0.||x2<0.||x3<0.)
		{
			sign_bit=1;
		}
		int fx1 = abs(int(x1));
		int fx2 = abs(int(x2));
		int fx3 = abs(int(x3));
		if(fx1>max_interger)
			max_interger=fx1;
		if(fx2>max_interger)
			max_interger=fx2;
		if(fx3>max_interger)
			max_interger=fx3;
	}
	int interger_bit = 0;
	if(max_interger>0)
		interger_bit = 1;
	if(max_interger>1)
		interger_bit = 2;
	if(max_interger>3)
		interger_bit = 3;
	if(max_interger>7)
		if(sign_bit)
			interger_bit = 15;
		else
			interger_bit = 4;
	if(max_interger>15)
		interger_bit = 15;

	if(interger_bit==15)
	{
		sign_bit = 1;
		frag_bit = 16;
	}
	else
		frag_bit = 16-sign_bit-interger_bit;
	FIXED_POINT_FORMAT type = FIXED_POINT_FORMAT( (sign_bit<<16)+ (interger_bit<<8)+frag_bit);
	return type;
}

FIXED_POINT_FORMAT findFixedPointFormat(arrayVec2& a)
{
	int sign_bit = 0;
	int max_interger = 0;
	int frag_bit;
	for(int i=0;i< a.size();i++)
	{
		float x1 = a[i].x;
		float x2 = a[i].y;
		if(x1<0.||x2<0.)
		{
			sign_bit=1;
		}
		int fx1 = abs(int(x1));
		int fx2 = abs(int(x2));
		if(fx1>max_interger)
			max_interger=fx1;
		if(fx2>max_interger)
			max_interger=fx1;
	}
	int interger_bit = 0;
	if(max_interger>0)
		interger_bit = 1;
	if(max_interger>1)
		interger_bit = 2;
	if(max_interger>3)
		interger_bit = 3;
	if(max_interger>7)
		if(sign_bit)
			interger_bit = 15;
		else
			interger_bit = 4;
	if(max_interger>15)
		interger_bit = 15;
	if(interger_bit==15)
	{
		sign_bit = 1;
		frag_bit = 16;
	}
	else
		frag_bit = 16-sign_bit-interger_bit;
	FIXED_POINT_FORMAT type = FIXED_POINT_FORMAT( (sign_bit<<16)+ (interger_bit<<8)+frag_bit);
	return type;
}
string getFixedPointStr(FIXED_POINT_FORMAT type)
{
	string temp;
	int sign_bits = type>>16;
	int integer_bits= type>>8&0xff;
	int fractional_bits = type&0xff;
	char temp1[10];
	char temp2[10];
	char temp3[10];
	temp.append("FP_");
	temp.append(itoa(sign_bits,temp1,10));
	temp.append("_");
	temp.append(itoa(integer_bits,temp2,10));
	temp.append("_");
	temp.append(itoa(fractional_bits,temp3,10));
	return temp;
}
int getFixedPointSize(FIXED_POINT_FORMAT type)
{
	int sign_bits = type>>16;
	int integer_bits= type>>8&0xff;
	int fractional_bits = type&0xff;
	return (sign_bits+integer_bits+fractional_bits)/8;
}